<?php

namespace Action\User\Picture;

class ByUpload extends \Library\Action\UserAction {
	
	private $_imagesMimes = array(
		'image/jpeg',
		'image/png',
		'image/gif'
	);
	private $_picture;

	public function execute() {
		parent::execute();
		
		header( 'Content-Type: text/html' );
		
		try {
			$this->setView( 'Action/User/PictureDialog/SizePanel' );
		
			_a( !$_FILES['image']['error'], 'Upload failed' );
			_a( in_array($_FILES['image']['type'], $this->_imagesMimes), 'You must upload an image' );
		
			$content = file_get_contents($_FILES['image']['tmp_name']);

			$this->_picture = new \Library\Picture( $content );
		
			$json = json_encode( array(
				'status' => 'success',
				'content' => $this->view(),
				'time' => microtime(true)
			) );
			
		}
		catch( \Exception $e ) {
			$json = json_encode( array(
				'status' => 'exception',
				'message' => $e->getMessage()
			) );
		}
		die( '<script type="text/javascript">var json = ' . $json . ';</script>' );
		exit(0);
	}

	public function getPicture() {
		return $this->_picture;
	}
	
}
